package com.edou.cms.controller.admin;

import java.util.Map;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.json.simple.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.cms.form.CmsColumnArticleForm;
import com.edou.cms.entity.CmsColumnArticle;
import com.edou.cms.service.ICmsColumnArticleService;
import com.edou.base.token.TokenValidate;
import com.edou.base.controller.BaseController;


@Controller("cmsColumnArticleController")
@Scope("prototype")
@RequestMapping("/admin/cms/column/article")
public class CmsColumnArticleController extends BaseController {

	@Resource(name = "cmsColumnArticleServiceImpl")
	private ICmsColumnArticleService cmsColumnArticleService;



	/**
	 * 首页
	 * @return 
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public String index(){
		return "/admin/cms/cms_column_article_index";
	}

	/**
	 * 分页查询
	 * @return 
	 */
	@RequestMapping(value = "/listCmsColumnArticleByPage.htm", method = RequestMethod.POST)
	public void listCmsColumnArticleByPage(@ModelAttribute CmsColumnArticleForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try{
			cmsColumnArticleService.listCmsColumnArticleByPage(model.getPageResult(),model.getCondition());
			JSONObject root = toPageJson(model.getPageResult(), new String[]{"id","columnId","columnSection","articleId"});
			ajaxPageResult(root,response);
		}catch(Exception ex){
			String msg = "查询专栏内的相关文章时发生异常："+ex.getMessage();
			logger.error(msg,ex);
		}
	}

	/**
	 * 保存专栏内的相关文章
	 * @return 
	 */
	@RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
	public ModelAndView edit(@ModelAttribute CmsColumnArticleForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/cms/cms_column_article_add");
		Map<String,Object> data = view.getModel();
		if(StringUtils.isNotBlank(model.getItem().getId())){
			CmsColumnArticle item = cmsColumnArticleService.getCmsColumnArticleById(model.getItem().getId());
			data.put("item", item);
		}

		return view;
	}

	/**
	 * 保存专栏内的相关文章
	 * @return 
	 */
	@RequestMapping(value = "/saveCmsColumnArticle.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView saveRole(@ModelAttribute CmsColumnArticleForm model,HttpServletRequest request,HttpSession session){
		try {
			if(StringUtils.isBlank(model.getItem().getId())){
				cmsColumnArticleService.add(model.getItem());
			}else{
				cmsColumnArticleService.update(model.getItem());
			}
			return ajaxJSON(Status.success,"保存成功");
		} catch (Exception e) {
			String msg = "保存专栏内的相关文章时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试");
		}
	}
	
	/**
	 * 批量保存专栏内的相关文章
	 * @return 
	 */
	@RequestMapping(value = "/saveCmsColumnArticleList.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView saveCmsColumnArticleList(@ModelAttribute CmsColumnArticleForm model,HttpServletRequest request,HttpSession session){
		try {
			cmsColumnArticleService.addArticleList(model.getSelIds().split(","), model.getColumnId(), model.getSectionId());
			return ajaxJSON(Status.success,"保存成功");
		} catch (Exception e) {
			String msg = "保存专栏内的相关文章时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试");
		}
	}
	/**
	 * 删除CmsColumnArticle
	 * @return 
	 */
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public ModelAndView del(@ModelAttribute CmsColumnArticleForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			cmsColumnArticleService.delByIds(model.getSelIds().split(","));
			return ajaxJSON(Status.success,"删除成功");
		} catch (Exception e) {
			String msg = "删除专栏内的相关文章时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "删除失败，请稍后重试");
		}
	}
	
	/**
	 * 编辑CmsColumnArticle
	 * @return 
	 */
	@RequestMapping(value = "/editCmsColumnArticle.htm", method = RequestMethod.POST)
	public ModelAndView editCmsColumnArticle(@ModelAttribute CmsColumnArticleForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			cmsColumnArticleService.update(model.getItem());
			return ajaxJSON(Status.success,"修改成功");
		} catch (Exception e) {
			String msg = "修改专栏内的相关文章时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "修改失败，请稍后重试");
		}
	}

}

